package com.mercury.ec.module.air.service.demandorder;

import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandprice.AirDemandPricePageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandprice.AirDemandPriceSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandPriceDO;
import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.common.util.object.BeanUtils;

import com.mercury.ec.module.air.dal.mysql.demandorder.AirDemandPriceMapper;

import static com.mercury.ec.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.mercury.ec.module.air.enums.ErrorCodeConstants.*;

/**
 * 机票需求订单方案价格 Service 实现类
 *
 * @author hesx
 */
@Service
@Validated
public class AirDemandPriceServiceImpl implements AirDemandPriceService {

    @Resource
    private AirDemandPriceMapper demandPriceMapper;

    @Override
    public Long createDemandPrice(AirDemandPriceSaveReqVO createReqVO) {
        // 插入
        AirDemandPriceDO demandPrice = BeanUtils.toBean(createReqVO, AirDemandPriceDO.class);
        demandPriceMapper.insert(demandPrice);
        // 返回
        return demandPrice.getId();
    }

    @Override
    public void updateDemandPrice(AirDemandPriceSaveReqVO updateReqVO) {
        // 校验存在
        validateDemandPriceExists(updateReqVO.getId());
        // 更新
        AirDemandPriceDO updateObj = BeanUtils.toBean(updateReqVO, AirDemandPriceDO.class);
        demandPriceMapper.updateById(updateObj);
    }

    @Override
    public void deleteDemandPrice(Long id) {
        // 校验存在
        validateDemandPriceExists(id);
        // 删除
        demandPriceMapper.deleteById(id);
    }

    private void validateDemandPriceExists(Long id) {
        if (demandPriceMapper.selectById(id) == null) {
            throw exception(AIR_DEMAND_PRICE_NOT_EXISTS);
        }
    }

    @Override
    public AirDemandPriceDO getDemandPrice(Long id) {
        return demandPriceMapper.selectById(id);
    }

    @Override
    public PageResult<AirDemandPriceDO> getDemandPricePage(AirDemandPricePageReqVO pageReqVO) {
        return demandPriceMapper.selectPage(pageReqVO);
    }

}
